<template>
  <div id="app">
    <div class="demo-container">
      <header class="demo-header">
        <h1>Vue 3 Tree 组件演示</h1>
        <p>这是一个功能完整的Vue 3树形组件，支持展开/收起、复选框选择、搜索过滤、拖拽排序等功能。</p>
      </header>

      <div class="demo-content">
        <!-- 控制面板 -->
        <div class="control-panel">
          <h3>控制面板</h3>
          <div class="control-grid">
            <label class="control-item">
              <input type="checkbox" v-model="showCheckbox" />
              <span class="control-text">显示复选框</span>
            </label>
            <label class="control-item">
              <input type="checkbox" v-model="showLine" />
              <span class="control-text">显示连接线</span>
            </label>
            <label class="control-item">
              <input type="checkbox" v-model="filterable" />
              <span class="control-text">启用搜索</span>
            </label>
            <label class="control-item">
              <input type="checkbox" v-model="draggable" />
              <span class="control-text">启用拖拽</span>
            </label>
          </div>
          <div class="control-buttons">
            <button class="btn btn-primary" @click="handleExpandAll">展开全部</button>
            <button class="btn btn-secondary" @click="handleCollapseAll">收起全部</button>
            <button class="btn btn-success" @click="handleGetCheckedNodes">获取选中节点</button>
            <button class="btn btn-warning" @click="handleClearChecked">清空选中</button>
          </div>
        </div>

        <div class="demo-main">
          <!-- 文件树 -->
          <div class="tree-container">
            <h3>文件树</h3>
            <Tree
              ref="treeRef"
              :data="treeData"
              :show-checkbox="showCheckbox"
              :show-line="showLine"
              :filterable="filterable"
              :draggable="draggable"
              :default-expand-all="false"
              :highlight-current="true"
              node-key="id"
              @node-click="handleNodeClick"
              @node-expand="handleNodeExpand"
              @node-collapse="handleNodeCollapse"
              @check-change="handleCheckChange"
              @node-drop="handleNodeDrop"
              @node-context-menu="handleNodeContextMenu"
            >
              <template #icon="{ node, expanded }">
                <svg v-if="node.type === 'folder'" class="node-icon" viewBox="0 0 24 24">
                  <path v-if="expanded" fill="#ffd700" d="M10 4H4c-1.11 0-2 .89-2 2v12c0 1.11.89 2 2 2h16c1.11 0 2-.89 2-2V8c0-1.11-.89-2-2-2h-8l-2-2z"/>
                  <path v-else fill="#ffb74d" d="M10 4H4c-1.11 0-2 .89-2 2v12c0 1.11.89 2 2 2h16c1.11 0 2-.89 2-2V8c0-1.11-.89-2-2-2h-8l-2-2z"/>
                </svg>
                <svg v-else class="node-icon" viewBox="0 0 24 24">
                  <path fill="#64b5f6" d="M14,2H6A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2M18,20H6V4H13V9H18V20Z"/>
                </svg>
              </template>
            </Tree>
          </div>

          <!-- 信息面板 -->
          <div class="info-panel">
            <h3>最后点击的节点</h3>
            <div v-if="selectedNode" class="node-info">
              <div class="info-item">
                <span class="info-label">名称:</span>
                <span class="info-value">{{ selectedNode.label }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">ID:</span>
                <span class="info-value">{{ selectedNode.id }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">类型:</span>
                <span class="info-value">{{ selectedNode.type }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">路径:</span>
                <span class="info-value">{{ selectedNode.path }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">子节点数:</span>
                <span class="info-value">{{ selectedNode.children ? selectedNode.children.length : 0 }}</span>
              </div>
            </div>
            <div v-else class="no-selection">
              暂无选中的节点
            </div>

            <h4>已勾选的节点 ({{ checkedNodes.length }})</h4>
            <div class="checked-nodes">
              <div v-if="checkedNodes.length === 0" class="no-checked">
                暂无勾选的节点
              </div>
              <div v-else class="checked-list">
                <div v-for="node in checkedNodes" :key="node.id" class="checked-item">
                  {{ node.label }}
                </div>
              </div>
            </div>

            <h4>使用说明</h4>
            <ul class="usage-tips">
              <li><strong>展开/收起:</strong> 点击节点前的箭头或双击节点</li>
              <li><strong>多选模式:</strong> 勾选复选框，支持父子节点联动</li>
              <li><strong>点击选中:</strong> 点击节点名称选中该节点</li>
              <li><strong>批量操作:</strong> 使用控制面板的按钮进行批量操作</li>
              <li><strong>搜索功能:</strong> 在搜索框中输入关键词过滤节点</li>
              <li><strong>拖拽排序:</strong> 启用拖拽后可以拖动节点重新排序</li>
              <li><strong>右键菜单:</strong> 右键点击节点显示上下文菜单</li>
              <li><strong>联动选择:</strong> 父节点的选择状态会影响子节点</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'
import Tree from './components/Tree/Tree.vue'

// 响应式数据
const treeRef = ref(null)
const selectedNode = ref(null)
const checkedNodes = ref([])

// 控制选项 - 确保响应式
const showCheckbox = ref(true)
const showLine = ref(false)
const filterable = ref(true)
const draggable = ref(false)

// 树形数据
const treeData = reactive([
  {
    id: '1',
    label: '项目根目录',
    type: 'folder',
    path: '/',
    children: [
      {
        id: '1-1',
        label: 'src',
        type: 'folder',
        path: '/src',
        children: [
          {
            id: '1-1-1',
            label: 'components',
            type: 'folder',
            path: '/src/components',
            children: [
              {
                id: '1-1-1-1',
                label: 'Tree.vue',
                type: 'file',
                path: '/src/components/Tree.vue'
              },
              {
                id: '1-1-1-2',
                label: 'TreeNode.vue',
                type: 'file',
                path: '/src/components/TreeNode.vue'
              }
            ]
          },
          {
            id: '1-1-2',
            label: 'views',
            type: 'folder',
            path: '/src/views',
            children: [
              {
                id: '1-1-2-1',
                label: 'Home.vue',
                type: 'file',
                path: '/src/views/Home.vue'
              },
              {
                id: '1-1-2-2',
                label: 'About.vue',
                type: 'file',
                path: '/src/views/About.vue'
              }
            ]
          },
          {
            id: '1-1-3',
            label: 'assets',
            type: 'folder',
            path: '/src/assets',
            children: [
              {
                id: '1-1-3-1',
                label: 'logo.png',
                type: 'file',
                path: '/src/assets/logo.png'
              },
              {
                id: '1-1-3-2',
                label: 'style.css',
                type: 'file',
                path: '/src/assets/style.css'
              }
            ]
          }
        ]
      },
      {
        id: '1-2',
        label: 'public',
        type: 'folder',
        path: '/public',
        children: [
          {
            id: '1-2-1',
            label: 'index.html',
            type: 'file',
            path: '/public/index.html'
          },
          {
            id: '1-2-2',
            label: 'favicon.ico',
            type: 'file',
            path: '/public/favicon.ico'
          }
        ]
      },
      {
        id: '1-3',
        label: 'package.json',
        type: 'file',
        path: '/package.json'
      },
      {
        id: '1-4',
        label: 'README.md',
        type: 'file',
        path: '/README.md'
      }
    ]
  }
])

// 事件处理
const handleNodeClick = (node) => {
  selectedNode.value = node
  console.log('节点点击:', node)
}

const handleNodeExpand = (node) => {
  console.log('节点展开:', node)
}

const handleNodeCollapse = (node) => {
  console.log('节点折叠:', node)
}

const handleCheckChange = (node, checked, checkedKeys) => {
  console.log('选中状态改变:', node, checked, checkedKeys)
  updateCheckedNodes()
}

const handleNodeDrop = (dragNode, dropNode, position) => {
  console.log('节点拖拽:', dragNode, dropNode, position)
}

const handleNodeContextMenu = (node, event) => {
  console.log('右键菜单:', node, event)
}

const handleExpandAll = () => {
  treeRef.value?.expandAllNodes()
}

const handleCollapseAll = () => {
  treeRef.value?.collapseAllNodes()
}

const handleGetCheckedNodes = () => {
  const nodes = treeRef.value?.getCheckedNodes()
  console.log('选中的节点:', nodes)
  alert(`已选中 ${nodes?.length || 0} 个节点，请查看控制台`)
}

const handleClearChecked = () => {
  treeRef.value?.setCheckedKeys([])
  updateCheckedNodes()
}

const updateCheckedNodes = () => {
  checkedNodes.value = treeRef.value?.getCheckedNodes() || []
}

// 监听控制选项变化
watch([showCheckbox, showLine, filterable, draggable], () => {
  console.log('控制选项变化:', {
    showCheckbox: showCheckbox.value,
    showLine: showLine.value,
    filterable: filterable.value,
    draggable: draggable.value
  })
})
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #f8f9fa;
}

#app {
  min-height: 100vh;
  width: 100%;
}

.demo-container {
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  padding: 20px;
}

.demo-header {
  text-align: center;
  margin-bottom: 32px;
  padding: 48px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.demo-header h1 {
  font-size: 2.8rem;
  font-weight: 700;
  margin-bottom: 16px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.demo-header p {
  font-size: 1.2rem;
  opacity: 0.9;
  max-width: 700px;
  margin: 0 auto;
  line-height: 1.8;
}

.demo-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.control-panel {
  background: white;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid #e9ecef;
}

.control-panel h3 {
  margin-bottom: 24px;
  color: #495057;
  font-size: 1.4rem;
  font-weight: 600;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.control-item {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-size: 16px;
  color: #495057;
  user-select: none;
  padding: 12px 16px;
  border-radius: 8px;
  transition: all 0.2s ease;
  border: 2px solid transparent;
}

.control-item:hover {
  background-color: #f8f9fa;
  border-color: #e9ecef;
}

.control-item input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: #409eff;
  cursor: pointer;
}

.control-text {
  font-weight: 500;
}

.control-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 16px;
}

.btn {
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
}

.btn-primary {
  background-color: #409eff;
  color: white;
}

.btn-primary:hover {
  background-color: #337ecc;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.4);
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #545b62;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(108, 117, 125, 0.4);
}

.btn-success {
  background-color: #67c23a;
  color: white;
}

.btn-success:hover {
  background-color: #529b2e;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(103, 194, 58, 0.4);
}

.btn-warning {
  background-color: #e6a23c;
  color: white;
}

.btn-warning:hover {
  background-color: #b88230;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(230, 162, 60, 0.4);
}

.demo-main {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 24px;
  align-items: start;
}

.tree-container {
  background: white;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid #e9ecef;
  min-height: 600px;
}

.tree-container h3 {
  margin-bottom: 24px;
  color: #495057;
  font-size: 1.4rem;
  font-weight: 600;
  border-bottom: 2px solid #e9ecef;
  padding-bottom: 16px;
}

.info-panel {
  background: white;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid #e9ecef;
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
}

.info-panel h3, .info-panel h4 {
  margin-bottom: 20px;
  color: #495057;
  font-size: 1.2rem;
  font-weight: 600;
}

.info-panel h4 {
  margin-top: 32px;
  font-size: 1.1rem;
  border-top: 1px solid #e9ecef;
  padding-top: 20px;
}

.node-info {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 12px;
  border: 1px solid #e9ecef;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #e9ecef;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  font-weight: 600;
  color: #495057;
  min-width: 100px;
}

.info-value {
  color: #6c757d;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 14px;
  background: #f1f3f4;
  padding: 4px 8px;
  border-radius: 6px;
  word-break: break-all;
}

.no-selection, .no-checked {
  text-align: center;
  color: #6c757d;
  font-style: italic;
  padding: 24px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 2px dashed #dee2e6;
}

.checked-nodes {
  max-height: 250px;
  overflow-y: auto;
}

.checked-list {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 16px;
  border: 1px solid #e9ecef;
}

.checked-item {
  padding: 8px 16px;
  margin-bottom: 8px;
  background: white;
  border-radius: 8px;
  font-size: 14px;
  color: #495057;
  border: 1px solid #e9ecef;
  display: flex;
  align-items: center;
  gap: 8px;
}

.checked-item:last-child {
  margin-bottom: 0;
}

.usage-tips {
  list-style: none;
  padding: 0;
}

.usage-tips li {
  padding: 12px 0;
  border-bottom: 1px solid #f1f3f4;
  font-size: 15px;
  line-height: 1.6;
  color: #495057;
}

.usage-tips li:last-child {
  border-bottom: none;
}

.usage-tips strong {
  color: #409eff;
  font-weight: 600;
}

.node-icon {
  width: 18px;
  height: 18px;
}

/* 平板适配 */
@media (max-width: 1024px) {
  .demo-main {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  
  .info-panel {
    position: static;
    max-height: none;
  }
  
  .control-grid {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .demo-container {
    padding: 16px;
  }
  
  .demo-header {
    padding: 32px 20px;
    margin-bottom: 24px;
  }
  
  .demo-header h1 {
    font-size: 2.2rem;
  }
  
  .demo-header p {
    font-size: 1.1rem;
  }
  
  .control-panel, .tree-container, .info-panel {
    padding: 24px;
  }
  
  .control-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .control-item {
    padding: 16px;
    font-size: 18px;
  }
  
  .control-item input[type="checkbox"] {
    width: 24px;
    height: 24px;
  }
  
  .control-buttons {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .btn {
    padding: 16px 24px;
    font-size: 18px;
    min-height: 56px;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .info-value {
    align-self: stretch;
    font-size: 16px;
  }
  
  .usage-tips li {
    font-size: 16px;
    padding: 16px 0;
  }
  
  .node-icon {
    width: 20px;
    height: 20px;
  }
}

/* 小屏手机适配 */
@media (max-width: 480px) {
  .demo-container {
    padding: 12px;
  }
  
  .demo-header h1 {
    font-size: 1.8rem;
  }
  
  .demo-header p {
    font-size: 1rem;
  }
  
  .control-panel, .tree-container, .info-panel {
    padding: 20px;
  }
  
  .control-panel h3, .tree-container h3, .info-panel h3 {
    font-size: 1.2rem;
  }
  
  .btn {
    font-size: 16px;
    padding: 14px 20px;
  }
}
</style>